﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using ASPNETVN.PORTAL.Components.Configuration;
using ASPNETVN.PORTAL.Components.Configuration.Settings;
using System.Web.UI.WebControls;

namespace ASPNETVN.PORTAL.Components.UI
{
    public class ItemControl : UserControl
    {
        private readonly PlaceHolder _header = new PlaceHolder();
        private readonly PlaceHolder _footer = new PlaceHolder();
        private readonly PlaceHolder _headerPlaceHolder = new PlaceHolder();

        private Page _ParentPage;
        public Page ParentPage
        {
            get
            {
                if (this._ParentPage != null)
                {
                    return this._ParentPage;
                }

                Page control = (Page)this.Page;
                this._ParentPage = (Page)control;
                return this._ParentPage;
            }
        }

        public void ReturnToReferrerPage()
        {
            this.ParentPage.ReturnToReferrerPage();
        }

        #region Stardand Controls

        /// <summary>
        /// Standard update button.
        /// </summary>
        protected System.Web.UI.WebControls.LinkButton ButtonUpdate;

        /// <summary>
        /// Standard delete button.
        /// </summary>
        protected System.Web.UI.WebControls.LinkButton ButtonDelete;

        /// <summary>
        /// Standard cancel button.
        /// </summary>
        protected System.Web.UI.WebControls.LinkButton ButtonCancel;

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            // Create frame for module content.
            LiteralControl literalControl12345 = new LiteralControl();
            literalControl12345.Text = "<div class='ModuleContent'><table width='100%' cellpadding='0' cellspacing='0' border='0'><tr><td class='ModuleFrame1'></td><td class='ModuleFrame2'></td><td class='ModuleFrame3'></td></tr><tr><td class='ModuleFrame4'></td><td class='ModuleFrame5'>";

            LiteralControl literalControl6789 = new LiteralControl();
            literalControl6789.Text = "</td><td class='ModuleFrame6'></td></tr><tr><td class='ModuleFrame7'></td><td class='ModuleFrame8'></td><td class='ModuleFrame9'></td></tr></table></div>";

            _header.Controls.Add(literalControl12345);
            _footer.Controls.Add(literalControl6789);

            this._headerPlaceHolder.Controls.Add(_header);
            this.Controls.Add(_footer);


        }

        protected override void OnInit(EventArgs e)
        {
            if (this.ButtonCancel != null)
            {
                this.ButtonCancel.Click += this.ButtonCancel_Click;
                this.ButtonCancel.Text = Global.RM.GetString("BUTTON_GOBACK");
                this.ButtonCancel.CausesValidation = false;
                this.ButtonCancel.EnableViewState = false;
            }

            this.Controls.AddAt(0, _headerPlaceHolder);

            base.OnInit(e);
        }

        #region Custom events.

        // Cancel event.
        public event EventHandler Cancel;
        protected virtual void OnCancel(EventArgs e)
        {
            if (Cancel != null)
            {
                Cancel(this, e);
            }
        }

        void ButtonCancel_Click(Object sender, EventArgs e)
        {
            this.OnCancel(e);
            ((Page)this.Page).ReturnToReferrerPage();
        }

        #endregion

        #region Properties (Tabs)

        private int _tabID;
        private int _tabIndex;

        public int TabID
        {
            get
            {
                if (this._tabID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["TabID"] != null)
                        this._tabID = Int32.Parse(Request.Params["TabID"]);
                }
                return this._tabID;
            }
        }

        public int TabIndex
        {
            get
            {
                if (this._tabIndex == 0)
                {
                    if (HttpContext.Current != null && Request.Params["TabIndex"] != null)
                        this._tabIndex = Int32.Parse(Request.Params["TabIndex"]);
                }
                return this._tabIndex;
            }
        }

        #endregion

        #region Properties (Items)

        private int _itemID;

        public int ItemID
        {
            get
            {
                if (_itemID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["ItemID"] != null)
                        _itemID = Int32.Parse(Request.Params["ItemID"]);
                }
                return _itemID;
            }
            set { _itemID = value; }
        }

        #endregion

        #region Properties (Modules)

        private int _moduleID;
        private Hashtable _settings;

        public int ModuleID
        {
            get
            {
                if (_moduleID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["Mid"] != null)
                        _moduleID = Int32.Parse(Request.Params["Mid"]);
                }
                return _moduleID;
            }
        }

        public Hashtable Settings
        {
            get
            {
                if (this._settings == null)
                {
                    if (ModuleID > 0)
                    {
                        this._settings = PortalConfiguration.GetModuleSettings(ModuleID, this.Page);
                    }
                    else
                    {
                        this._settings = new Hashtable();
                    }
                }

                return this._settings;
            }
        }

        #endregion

        #region Portal

        public Configuration.Items.Portal Portal
        {
            get { return (Configuration.Items.Portal)HttpContext.Current.Items["Portal"]; }
        }

        #endregion
    }
}